package com.kl.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kl.domain.StaffWarehouseDTO;
import com.kl.em.ConstantEnum;
import com.kl.em.Result;
import com.kl.entity.StaffWarehouse;
import com.kl.service.IStaffWarehouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2021-10-30
 */
//仓库的管理
@RestController
@RequestMapping("/staff-warehouse")
@Api(value = "物流站点仓库管理功能接口")
@RefreshScope //nacos配置中心 开启自动刷新配置
public class StaffWarehouseController {
    @Resource
    private IStaffWarehouseService iStaffWarehouseService;

    @ApiOperation("根据仓库ID进行查询")
    @GetMapping("selectStaffWarehouseById/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "站点仓库ID",required = true, paramType = "path")
    })
    public Result<StaffWarehouseDTO> selectStaffWarehouseById(@PathVariable Long id){

        StaffWarehouseDTO staffWarehouseDTO = new StaffWarehouseDTO();
        StaffWarehouse byId = iStaffWarehouseService.getById(id);
        BeanUtils.copyProperties(byId,staffWarehouseDTO);

        return Result.build(ConstantEnum.SUCCESS).setResult(staffWarehouseDTO);
    }

    @ApiOperation("根据站点ID进行查询")
    @GetMapping("selectStaffWarehouseByLogisticsStoreId/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "站点ID",required = true, paramType = "path")
    })
    public Result<List<StaffWarehouseDTO>> selectStaffWarehouseByLogisticsStoreId(@PathVariable Long id){
        List<StaffWarehouseDTO> staffWarehouseDTOList = new ArrayList<>();

        LambdaQueryWrapper<StaffWarehouse> eq = Wrappers.<StaffWarehouse>lambdaQuery()
                .eq(StaffWarehouse::getLid, id);

        List<StaffWarehouse> list = iStaffWarehouseService.list(eq);

        for (StaffWarehouse staffWarehouse : list) {

            StaffWarehouseDTO staffWarehouseDTO = new StaffWarehouseDTO();
            BeanUtils.copyProperties(staffWarehouse,staffWarehouseDTO);
            staffWarehouseDTOList.add(staffWarehouseDTO);
        }

        return Result.build(ConstantEnum.SUCCESS).setResult(staffWarehouseDTOList);
    }

    @ApiOperation("站点仓库增加")
    @PostMapping("saveStaffWarehouse")
    @ApiImplicitParams({
            @ApiImplicitParam(name="staffWarehouseDTO",value = "站点创库的实体类",required = true, paramType = "body")
    })
    public Result saveStaffWarehouse(@RequestBody StaffWarehouseDTO staffWarehouseDTO){

        if (staffWarehouseDTO == null || StringUtils.isBlank(staffWarehouseDTO.toString())){
            return Result.build(ConstantEnum.ARGS_NULL);
        }

        StaffWarehouse staffWarehouse = new StaffWarehouse();
        BeanUtils.copyProperties(staffWarehouseDTO,staffWarehouse);
        staffWarehouse.insert();
        return Result.build(ConstantEnum.SUCCESS);
    }

    @ApiOperation("站点仓库修改(包括修改仓库状态)")
    @PutMapping("updateStaffWarehouse/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name="staffWarehouseDTO",value = "站点的实体类",required = true, paramType = "body"),
            @ApiImplicitParam(name="id",value = "实体类的ID",required = true, paramType = "path"),
            @ApiImplicitParam(name="warehouseStatus",value = "仓库状态更改(0运营,1关闭,2满仓)",required = false, paramType = "query")
    })
    public Result updateStaffWarehouse(@PathVariable Long id,@RequestBody StaffWarehouseDTO staffWarehouseDTO,@RequestParam(required = false) Integer warehouseStatus){

        if (staffWarehouseDTO == null || StringUtils.isBlank(staffWarehouseDTO.toString())){
            return Result.build(ConstantEnum.ARGS_NULL);
        }

        StaffWarehouse staffWarehouse = new StaffWarehouse();
        BeanUtils.copyProperties(staffWarehouseDTO,staffWarehouse);
        staffWarehouse.setSwIdFurl(id);

        if (StringUtils.isNotEmpty(warehouseStatus.toString()) && null != warehouseStatus){
            staffWarehouse.setWarehouseStatus(warehouseStatus);
        }

        staffWarehouse.updateById();

        return Result.build(ConstantEnum.SUCCESS);
    }
    @ApiOperation("站点仓库删除(物理删除)")
    @DeleteMapping("deleteStaffWarehouse")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "实体类的ID(或数组)",required = true, paramType = "body")
    })
    public Result deleteStaffWarehouse(@RequestBody Long[] id){
        List<Long> list = new ArrayList<>();
        for (Long aLong : id) {
            list.add(aLong);
        }
        boolean b = iStaffWarehouseService.removeByIds(list);

        return Result.build(ConstantEnum.SUCCESS);
    }



    //kl增加的站点仓库查询所有
    @ApiOperation("站点仓库查询所有")
    @GetMapping("/list")
    public Result<List<StaffWarehouseDTO>> selectStaffWarehouseToList(){
        List<StaffWarehouse> list = iStaffWarehouseService.list();
        String s = JSONObject.toJSONString(list);
        List<StaffWarehouseDTO> dtos = JSONObject.parseArray(s, StaffWarehouseDTO.class);
        return Result.build(ConstantEnum.SUCCESS).setResult(dtos);
    }
}
